{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import joblib\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.metrics import classification_report,roc_auc_score"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T07:01:14.260122700Z",
     "start_time": "2025-06-11T07:01:14.249863600Z"
    }
   },
   "id": "5a8a803fd48a5d"
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "   Age     BusinessTravel              Department  DistanceFromHome  \\\n0   40         Non-Travel  Research & Development                 9   \n1   53      Travel_Rarely  Research & Development                 7   \n2   42      Travel_Rarely  Research & Development                 2   \n3   34  Travel_Frequently         Human Resources                11   \n4   32      Travel_Rarely  Research & Development                 1   \n\n   Education EducationField  EmployeeNumber  EnvironmentSatisfaction  Gender  \\\n0          4          Other            1449                        3    Male   \n1          2        Medical            1201                        4  Female   \n2          4          Other             477                        1    Male   \n3          3  Life Sciences            1289                        3    Male   \n4          1  Life Sciences             134                        4    Male   \n\n   JobInvolvement  ...  StandardHours StockOptionLevel  TotalWorkingYears  \\\n0               3  ...             80                2                 11   \n1               3  ...             80                1                 26   \n2               2  ...             80                0                 14   \n3               2  ...             80                2                 14   \n4               3  ...             80                0                  1   \n\n  TrainingTimesLastYear  WorkLifeBalance  YearsAtCompany YearsInCurrentRole  \\\n0                     2                4               8                  7   \n1                     6                3               7                  7   \n2                     6                3               1                  0   \n3                     5                4              10                  9   \n4                     2                3               1                  0   \n\n  YearsSinceLastPromotion  YearsWithCurrManager  Attrition  \n0                       0                     7          0  \n1                       4                     7          0  \n2                       0                     0          0  \n3                       1                     8          0  \n4                       0                     0          0  \n\n[5 rows x 31 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Age</th>\n      <th>BusinessTravel</th>\n      <th>Department</th>\n      <th>DistanceFromHome</th>\n      <th>Education</th>\n      <th>EducationField</th>\n      <th>EmployeeNumber</th>\n      <th>EnvironmentSatisfaction</th>\n      <th>Gender</th>\n      <th>JobInvolvement</th>\n      <th>...</th>\n      <th>StandardHours</th>\n      <th>StockOptionLevel</th>\n      <th>TotalWorkingYears</th>\n      <th>TrainingTimesLastYear</th>\n      <th>WorkLifeBalance</th>\n      <th>YearsAtCompany</th>\n      <th>YearsInCurrentRole</th>\n      <th>YearsSinceLastPromotion</th>\n      <th>YearsWithCurrManager</th>\n      <th>Attrition</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>40</td>\n      <td>Non-Travel</td>\n      <td>Research &amp; Development</td>\n      <td>9</td>\n      <td>4</td>\n      <td>Other</td>\n      <td>1449</td>\n      <td>3</td>\n      <td>Male</td>\n      <td>3</td>\n      <td>...</td>\n      <td>80</td>\n      <td>2</td>\n      <td>11</td>\n      <td>2</td>\n      <td>4</td>\n      <td>8</td>\n      <td>7</td>\n      <td>0</td>\n      <td>7</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>53</td>\n      <td>Travel_Rarely</td>\n      <td>Research &amp; Development</td>\n      <td>7</td>\n      <td>2</td>\n      <td>Medical</td>\n      <td>1201</td>\n      <td>4</td>\n      <td>Female</td>\n      <td>3</td>\n      <td>...</td>\n      <td>80</td>\n      <td>1</td>\n      <td>26</td>\n      <td>6</td>\n      <td>3</td>\n      <td>7</td>\n      <td>7</td>\n      <td>4</td>\n      <td>7</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>42</td>\n      <td>Travel_Rarely</td>\n      <td>Research &amp; Development</td>\n      <td>2</td>\n      <td>4</td>\n      <td>Other</td>\n      <td>477</td>\n      <td>1</td>\n      <td>Male</td>\n      <td>2</td>\n      <td>...</td>\n      <td>80</td>\n      <td>0</td>\n      <td>14</td>\n      <td>6</td>\n      <td>3</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>Travel_Frequently</td>\n      <td>Human Resources</td>\n      <td>11</td>\n      <td>3</td>\n      <td>Life Sciences</td>\n      <td>1289</td>\n      <td>3</td>\n      <td>Male</td>\n      <td>2</td>\n      <td>...</td>\n      <td>80</td>\n      <td>2</td>\n      <td>14</td>\n      <td>5</td>\n      <td>4</td>\n      <td>10</td>\n      <td>9</td>\n      <td>1</td>\n      <td>8</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>32</td>\n      <td>Travel_Rarely</td>\n      <td>Research &amp; Development</td>\n      <td>1</td>\n      <td>1</td>\n      <td>Life Sciences</td>\n      <td>134</td>\n      <td>4</td>\n      <td>Male</td>\n      <td>3</td>\n      <td>...</td>\n      <td>80</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 31 columns</p>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "real_data=pd.read_csv(\"../data/test2.csv\")\n",
    "real_data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:40:52.591681600Z",
     "start_time": "2025-06-11T06:40:52.552063300Z"
    }
   },
   "id": "1451bd5302aa42c8"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 对预测数据进行预处理"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "57617bc49e66fe30"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "# 加载筛选后的特征\n",
    "selected_features=joblib.load(\"../model/selected_features.pkl\")\n",
    "# 加载预处理对象 用来处理预测数据集\n",
    "one_hot_encoder=joblib.load(\"../model/OneHotEncoder.pkl\")\n",
    "skewed_features=joblib.load(\"../model/SkewedFeatures.pkl\")\n",
    "ss=joblib.load(\"../model/StandardScalerObject.pkl\")\n",
    "adasyn=joblib.load(\"../model/AdasynObject.pkl\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:44:16.550422900Z",
     "start_time": "2025-06-11T06:44:16.517363700Z"
    }
   },
   "id": "7310488043c35da5"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 数据预处理-特征筛选"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "63131dbf981e507c"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "   Attrition  Age  DistanceFromHome  Education  EnvironmentSatisfaction  \\\n0          0   40                 9          4                        3   \n1          0   53                 7          2                        4   \n2          0   42                 2          4                        1   \n3          0   34                11          3                        3   \n4          0   32                 1          1                        4   \n\n   JobInvolvement  JobSatisfaction  MonthlyIncome  NumCompaniesWorked  \\\n0               3                3           3975                   3   \n1               3                3          18606                   3   \n2               2                4           6781                   3   \n3               2                2           4490                   4   \n4               3                1           2956                   1   \n\n   PercentSalaryHike  ...  YearsInCurrentRole  YearsSinceLastPromotion  \\\n0                 11  ...                   7                        0   \n1                 18  ...                   7                        4   \n2                 23  ...                   0                        0   \n3                 11  ...                   9                        1   \n4                 13  ...                   0                        0   \n\n   YearsWithCurrManager     BusinessTravel              Department  \\\n0                     7         Non-Travel  Research & Development   \n1                     7      Travel_Rarely  Research & Development   \n2                     0      Travel_Rarely  Research & Development   \n3                     8  Travel_Frequently         Human Resources   \n4                     0      Travel_Rarely  Research & Development   \n\n   EducationField  Gender                    JobRole  MaritalStatus  OverTime  \n0           Other    Male      Laboratory Technician       Divorced        No  \n1         Medical  Female                    Manager       Divorced        No  \n2           Other    Male  Healthcare Representative         Single        No  \n3   Life Sciences    Male            Human Resources        Married        No  \n4   Life Sciences    Male         Research Scientist         Single        No  \n\n[5 rows x 27 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Attrition</th>\n      <th>Age</th>\n      <th>DistanceFromHome</th>\n      <th>Education</th>\n      <th>EnvironmentSatisfaction</th>\n      <th>JobInvolvement</th>\n      <th>JobSatisfaction</th>\n      <th>MonthlyIncome</th>\n      <th>NumCompaniesWorked</th>\n      <th>PercentSalaryHike</th>\n      <th>...</th>\n      <th>YearsInCurrentRole</th>\n      <th>YearsSinceLastPromotion</th>\n      <th>YearsWithCurrManager</th>\n      <th>BusinessTravel</th>\n      <th>Department</th>\n      <th>EducationField</th>\n      <th>Gender</th>\n      <th>JobRole</th>\n      <th>MaritalStatus</th>\n      <th>OverTime</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>40</td>\n      <td>9</td>\n      <td>4</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3975</td>\n      <td>3</td>\n      <td>11</td>\n      <td>...</td>\n      <td>7</td>\n      <td>0</td>\n      <td>7</td>\n      <td>Non-Travel</td>\n      <td>Research &amp; Development</td>\n      <td>Other</td>\n      <td>Male</td>\n      <td>Laboratory Technician</td>\n      <td>Divorced</td>\n      <td>No</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>53</td>\n      <td>7</td>\n      <td>2</td>\n      <td>4</td>\n      <td>3</td>\n      <td>3</td>\n      <td>18606</td>\n      <td>3</td>\n      <td>18</td>\n      <td>...</td>\n      <td>7</td>\n      <td>4</td>\n      <td>7</td>\n      <td>Travel_Rarely</td>\n      <td>Research &amp; Development</td>\n      <td>Medical</td>\n      <td>Female</td>\n      <td>Manager</td>\n      <td>Divorced</td>\n      <td>No</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>42</td>\n      <td>2</td>\n      <td>4</td>\n      <td>1</td>\n      <td>2</td>\n      <td>4</td>\n      <td>6781</td>\n      <td>3</td>\n      <td>23</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>Travel_Rarely</td>\n      <td>Research &amp; Development</td>\n      <td>Other</td>\n      <td>Male</td>\n      <td>Healthcare Representative</td>\n      <td>Single</td>\n      <td>No</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>34</td>\n      <td>11</td>\n      <td>3</td>\n      <td>3</td>\n      <td>2</td>\n      <td>2</td>\n      <td>4490</td>\n      <td>4</td>\n      <td>11</td>\n      <td>...</td>\n      <td>9</td>\n      <td>1</td>\n      <td>8</td>\n      <td>Travel_Frequently</td>\n      <td>Human Resources</td>\n      <td>Life Sciences</td>\n      <td>Male</td>\n      <td>Human Resources</td>\n      <td>Married</td>\n      <td>No</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>32</td>\n      <td>1</td>\n      <td>1</td>\n      <td>4</td>\n      <td>3</td>\n      <td>1</td>\n      <td>2956</td>\n      <td>1</td>\n      <td>13</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>Travel_Rarely</td>\n      <td>Research &amp; Development</td>\n      <td>Life Sciences</td>\n      <td>Male</td>\n      <td>Research Scientist</td>\n      <td>Single</td>\n      <td>No</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 27 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "real_data=real_data[selected_features]\n",
    "real_data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:44:49.556028800Z",
     "start_time": "2025-06-11T06:44:49.544451900Z"
    }
   },
   "id": "428a0011b4e89d71"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "X_real=real_data.iloc[:,1:]\n",
    "y_real=real_data.iloc[:,0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:50:57.974518700Z",
     "start_time": "2025-06-11T06:50:57.960981200Z"
    }
   },
   "id": "3b436e2fa20e5e75"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 特征工程"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "640a22cce6a51eaf"
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "   BusinessTravel_Non-Travel  BusinessTravel_Travel_Frequently  \\\n0                        1.0                               0.0   \n1                        0.0                               0.0   \n2                        0.0                               0.0   \n3                        0.0                               1.0   \n4                        0.0                               0.0   \n\n   BusinessTravel_Travel_Rarely  Department_Human Resources  \\\n0                           0.0                         0.0   \n1                           1.0                         0.0   \n2                           1.0                         0.0   \n3                           0.0                         1.0   \n4                           1.0                         0.0   \n\n   Department_Research & Development  Department_Sales  \\\n0                                1.0               0.0   \n1                                1.0               0.0   \n2                                1.0               0.0   \n3                                0.0               0.0   \n4                                1.0               0.0   \n\n   EducationField_Human Resources  EducationField_Life Sciences  \\\n0                             0.0                           0.0   \n1                             0.0                           0.0   \n2                             0.0                           0.0   \n3                             0.0                           1.0   \n4                             0.0                           1.0   \n\n   EducationField_Marketing  EducationField_Medical  ...  \\\n0                       0.0                     0.0  ...   \n1                       0.0                     1.0  ...   \n2                       0.0                     0.0  ...   \n3                       0.0                     0.0  ...   \n4                       0.0                     0.0  ...   \n\n   JobRole_Manufacturing Director  JobRole_Research Director  \\\n0                             0.0                        0.0   \n1                             0.0                        0.0   \n2                             0.0                        0.0   \n3                             0.0                        0.0   \n4                             0.0                        0.0   \n\n   JobRole_Research Scientist  JobRole_Sales Executive  \\\n0                         0.0                      0.0   \n1                         0.0                      0.0   \n2                         0.0                      0.0   \n3                         0.0                      0.0   \n4                         1.0                      0.0   \n\n   JobRole_Sales Representative  MaritalStatus_Divorced  \\\n0                           0.0                     1.0   \n1                           0.0                     1.0   \n2                           0.0                     0.0   \n3                           0.0                     0.0   \n4                           0.0                     0.0   \n\n   MaritalStatus_Married  MaritalStatus_Single  OverTime_No  OverTime_Yes  \n0                    0.0                   0.0          1.0           0.0  \n1                    0.0                   0.0          1.0           0.0  \n2                    0.0                   1.0          1.0           0.0  \n3                    1.0                   0.0          1.0           0.0  \n4                    0.0                   1.0          1.0           0.0  \n\n[5 rows x 28 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>BusinessTravel_Non-Travel</th>\n      <th>BusinessTravel_Travel_Frequently</th>\n      <th>BusinessTravel_Travel_Rarely</th>\n      <th>Department_Human Resources</th>\n      <th>Department_Research &amp; Development</th>\n      <th>Department_Sales</th>\n      <th>EducationField_Human Resources</th>\n      <th>EducationField_Life Sciences</th>\n      <th>EducationField_Marketing</th>\n      <th>EducationField_Medical</th>\n      <th>...</th>\n      <th>JobRole_Manufacturing Director</th>\n      <th>JobRole_Research Director</th>\n      <th>JobRole_Research Scientist</th>\n      <th>JobRole_Sales Executive</th>\n      <th>JobRole_Sales Representative</th>\n      <th>MaritalStatus_Divorced</th>\n      <th>MaritalStatus_Married</th>\n      <th>MaritalStatus_Single</th>\n      <th>OverTime_No</th>\n      <th>OverTime_Yes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 28 columns</p>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 独热编码\n",
    "features_str=real_data.select_dtypes(['object']).columns\n",
    "X_real_encoded=one_hot_encoder.transform(real_data[features_str])\n",
    "X_real_encoded=pd.DataFrame(data=X_real_encoded,columns=one_hot_encoder.get_feature_names_out(features_str),index=X_real.index)\n",
    "X_real_encoded.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:51:40.281208400Z",
     "start_time": "2025-06-11T06:51:40.266722400Z"
    }
   },
   "id": "f236b050d2cb0704"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "   Age  DistanceFromHome  Education  EnvironmentSatisfaction  JobInvolvement  \\\n0   40                 9          4                        3               3   \n1   53                 7          2                        4               3   \n2   42                 2          4                        1               2   \n3   34                11          3                        3               2   \n4   32                 1          1                        4               3   \n\n   JobSatisfaction  MonthlyIncome  NumCompaniesWorked  PercentSalaryHike  \\\n0                3           3975                   3                 11   \n1                3          18606                   3                 18   \n2                4           6781                   3                 23   \n3                2           4490                   4                 11   \n4                1           2956                   1                 13   \n\n   PerformanceRating  ...  JobRole_Manufacturing Director  \\\n0                  3  ...                             0.0   \n1                  3  ...                             0.0   \n2                  4  ...                             0.0   \n3                  3  ...                             0.0   \n4                  3  ...                             0.0   \n\n   JobRole_Research Director  JobRole_Research Scientist  \\\n0                        0.0                         0.0   \n1                        0.0                         0.0   \n2                        0.0                         0.0   \n3                        0.0                         0.0   \n4                        0.0                         1.0   \n\n   JobRole_Sales Executive  JobRole_Sales Representative  \\\n0                      0.0                           0.0   \n1                      0.0                           0.0   \n2                      0.0                           0.0   \n3                      0.0                           0.0   \n4                      0.0                           0.0   \n\n   MaritalStatus_Divorced  MaritalStatus_Married  MaritalStatus_Single  \\\n0                     1.0                    0.0                   0.0   \n1                     1.0                    0.0                   0.0   \n2                     0.0                    0.0                   1.0   \n3                     0.0                    1.0                   0.0   \n4                     0.0                    0.0                   1.0   \n\n   OverTime_No  OverTime_Yes  \n0          1.0           0.0  \n1          1.0           0.0  \n2          1.0           0.0  \n3          1.0           0.0  \n4          1.0           0.0  \n\n[5 rows x 47 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Age</th>\n      <th>DistanceFromHome</th>\n      <th>Education</th>\n      <th>EnvironmentSatisfaction</th>\n      <th>JobInvolvement</th>\n      <th>JobSatisfaction</th>\n      <th>MonthlyIncome</th>\n      <th>NumCompaniesWorked</th>\n      <th>PercentSalaryHike</th>\n      <th>PerformanceRating</th>\n      <th>...</th>\n      <th>JobRole_Manufacturing Director</th>\n      <th>JobRole_Research Director</th>\n      <th>JobRole_Research Scientist</th>\n      <th>JobRole_Sales Executive</th>\n      <th>JobRole_Sales Representative</th>\n      <th>MaritalStatus_Divorced</th>\n      <th>MaritalStatus_Married</th>\n      <th>MaritalStatus_Single</th>\n      <th>OverTime_No</th>\n      <th>OverTime_Yes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>40</td>\n      <td>9</td>\n      <td>4</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3</td>\n      <td>3975</td>\n      <td>3</td>\n      <td>11</td>\n      <td>3</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>53</td>\n      <td>7</td>\n      <td>2</td>\n      <td>4</td>\n      <td>3</td>\n      <td>3</td>\n      <td>18606</td>\n      <td>3</td>\n      <td>18</td>\n      <td>3</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>42</td>\n      <td>2</td>\n      <td>4</td>\n      <td>1</td>\n      <td>2</td>\n      <td>4</td>\n      <td>6781</td>\n      <td>3</td>\n      <td>23</td>\n      <td>4</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>11</td>\n      <td>3</td>\n      <td>3</td>\n      <td>2</td>\n      <td>2</td>\n      <td>4490</td>\n      <td>4</td>\n      <td>11</td>\n      <td>3</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>32</td>\n      <td>1</td>\n      <td>1</td>\n      <td>4</td>\n      <td>3</td>\n      <td>1</td>\n      <td>2956</td>\n      <td>1</td>\n      <td>13</td>\n      <td>3</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 47 columns</p>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_real=X_real.drop(features_str,axis=1)\n",
    "X_real=pd.concat([X_real,X_real_encoded],axis=1)\n",
    "X_real.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:53:07.813681Z",
     "start_time": "2025-06-11T06:53:07.800666500Z"
    }
   },
   "id": "502d947330b3352f"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 特征偏态处理"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "119b74cc0da51362"
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "for feature in skewed_features:\n",
    "    X_real[feature]=np.log1p(X_real[feature])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:54:21.018424900Z",
     "start_time": "2025-06-11T06:54:21.008020Z"
    }
   },
   "id": "55d2faf81f6d4cb7"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 数据标准化处理"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d740781148f052d4"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "        Age  DistanceFromHome  Education  EnvironmentSatisfaction  \\\n0  0.341102          0.360895   1.038605                 0.255040   \n1  1.787971          0.099762  -0.889607                 1.167379   \n2  0.563697         -1.048051   1.038605                -1.569638   \n3 -0.326684          0.574256   0.074499                 0.255040   \n4 -0.549279         -1.522545  -1.853713                 1.167379   \n\n   JobInvolvement  JobSatisfaction  MonthlyIncome  NumCompaniesWorked  \\\n0        0.421544         0.195284      -0.387596            0.467525   \n1        0.421544         0.195284       1.947129            0.467525   \n2       -0.939572         1.104542       0.420260            0.467525   \n3       -0.939572        -0.713974      -0.203333            0.800042   \n4        0.421544        -1.623233      -0.835553           -0.565368   \n\n   PercentSalaryHike  PerformanceRating  ...  JobRole_Manufacturing Director  \\\n0          -1.322555          -0.425685  ...                       -0.316228   \n1           0.835194          -0.425685  ...                       -0.316228   \n2           1.932140           2.349153  ...                       -0.316228   \n3          -1.322555          -0.425685  ...                       -0.316228   \n4          -0.598736          -0.425685  ...                       -0.316228   \n\n   JobRole_Research Director  JobRole_Research Scientist  \\\n0                  -0.234853                   -0.507093   \n1                  -0.234853                   -0.507093   \n2                  -0.234853                   -0.507093   \n3                  -0.234853                   -0.507093   \n4                  -0.234853                    1.972027   \n\n   JobRole_Sales Executive  JobRole_Sales Representative  \\\n0                -0.547588                     -0.229416   \n1                -0.547588                     -0.229416   \n2                -0.547588                     -0.229416   \n3                -0.547588                     -0.229416   \n4                -0.547588                     -0.229416   \n\n   MaritalStatus_Divorced  MaritalStatus_Married  MaritalStatus_Single  \\\n0                 1.86199              -0.914965             -0.686711   \n1                 1.86199              -0.914965             -0.686711   \n2                -0.53706              -0.914965              1.456217   \n3                -0.53706               1.092938             -0.686711   \n4                -0.53706              -0.914965              1.456217   \n\n   OverTime_No  OverTime_Yes  \n0     0.605359     -0.605359  \n1     0.605359     -0.605359  \n2     0.605359     -0.605359  \n3     0.605359     -0.605359  \n4     0.605359     -0.605359  \n\n[5 rows x 47 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Age</th>\n      <th>DistanceFromHome</th>\n      <th>Education</th>\n      <th>EnvironmentSatisfaction</th>\n      <th>JobInvolvement</th>\n      <th>JobSatisfaction</th>\n      <th>MonthlyIncome</th>\n      <th>NumCompaniesWorked</th>\n      <th>PercentSalaryHike</th>\n      <th>PerformanceRating</th>\n      <th>...</th>\n      <th>JobRole_Manufacturing Director</th>\n      <th>JobRole_Research Director</th>\n      <th>JobRole_Research Scientist</th>\n      <th>JobRole_Sales Executive</th>\n      <th>JobRole_Sales Representative</th>\n      <th>MaritalStatus_Divorced</th>\n      <th>MaritalStatus_Married</th>\n      <th>MaritalStatus_Single</th>\n      <th>OverTime_No</th>\n      <th>OverTime_Yes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.341102</td>\n      <td>0.360895</td>\n      <td>1.038605</td>\n      <td>0.255040</td>\n      <td>0.421544</td>\n      <td>0.195284</td>\n      <td>-0.387596</td>\n      <td>0.467525</td>\n      <td>-1.322555</td>\n      <td>-0.425685</td>\n      <td>...</td>\n      <td>-0.316228</td>\n      <td>-0.234853</td>\n      <td>-0.507093</td>\n      <td>-0.547588</td>\n      <td>-0.229416</td>\n      <td>1.86199</td>\n      <td>-0.914965</td>\n      <td>-0.686711</td>\n      <td>0.605359</td>\n      <td>-0.605359</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1.787971</td>\n      <td>0.099762</td>\n      <td>-0.889607</td>\n      <td>1.167379</td>\n      <td>0.421544</td>\n      <td>0.195284</td>\n      <td>1.947129</td>\n      <td>0.467525</td>\n      <td>0.835194</td>\n      <td>-0.425685</td>\n      <td>...</td>\n      <td>-0.316228</td>\n      <td>-0.234853</td>\n      <td>-0.507093</td>\n      <td>-0.547588</td>\n      <td>-0.229416</td>\n      <td>1.86199</td>\n      <td>-0.914965</td>\n      <td>-0.686711</td>\n      <td>0.605359</td>\n      <td>-0.605359</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.563697</td>\n      <td>-1.048051</td>\n      <td>1.038605</td>\n      <td>-1.569638</td>\n      <td>-0.939572</td>\n      <td>1.104542</td>\n      <td>0.420260</td>\n      <td>0.467525</td>\n      <td>1.932140</td>\n      <td>2.349153</td>\n      <td>...</td>\n      <td>-0.316228</td>\n      <td>-0.234853</td>\n      <td>-0.507093</td>\n      <td>-0.547588</td>\n      <td>-0.229416</td>\n      <td>-0.53706</td>\n      <td>-0.914965</td>\n      <td>1.456217</td>\n      <td>0.605359</td>\n      <td>-0.605359</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>-0.326684</td>\n      <td>0.574256</td>\n      <td>0.074499</td>\n      <td>0.255040</td>\n      <td>-0.939572</td>\n      <td>-0.713974</td>\n      <td>-0.203333</td>\n      <td>0.800042</td>\n      <td>-1.322555</td>\n      <td>-0.425685</td>\n      <td>...</td>\n      <td>-0.316228</td>\n      <td>-0.234853</td>\n      <td>-0.507093</td>\n      <td>-0.547588</td>\n      <td>-0.229416</td>\n      <td>-0.53706</td>\n      <td>1.092938</td>\n      <td>-0.686711</td>\n      <td>0.605359</td>\n      <td>-0.605359</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>-0.549279</td>\n      <td>-1.522545</td>\n      <td>-1.853713</td>\n      <td>1.167379</td>\n      <td>0.421544</td>\n      <td>-1.623233</td>\n      <td>-0.835553</td>\n      <td>-0.565368</td>\n      <td>-0.598736</td>\n      <td>-0.425685</td>\n      <td>...</td>\n      <td>-0.316228</td>\n      <td>-0.234853</td>\n      <td>1.972027</td>\n      <td>-0.547588</td>\n      <td>-0.229416</td>\n      <td>-0.53706</td>\n      <td>-0.914965</td>\n      <td>1.456217</td>\n      <td>0.605359</td>\n      <td>-0.605359</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 47 columns</p>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_numeric=X_real.select_dtypes(['int64','float64']).columns\n",
    "X_real[features_numeric]=ss.transform(X_real[features_numeric])\n",
    "X_real.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:55:48.047138500Z",
     "start_time": "2025-06-11T06:55:48.030482Z"
    }
   },
   "id": "b5ae7c68d022e80f"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 模型预测"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "148d05831c6c0bca"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 逻辑回归模型"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "de136e7a4fe2f315"
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [],
   "source": [
    "logit=joblib.load(\"../model/logit.pkl\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T07:30:50.198210500Z",
     "start_time": "2025-06-11T07:30:50.085520400Z"
    }
   },
   "id": "3b0130b8c372327a"
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [],
   "source": [
    "logit_predict=logit.predict(X_real)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T07:30:50.605472800Z",
     "start_time": "2025-06-11T07:30:50.597454500Z"
    }
   },
   "id": "d13d26ab9644f260"
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.97      0.77      0.86       297\n",
      "           1       0.39      0.85      0.54        53\n",
      "\n",
      "    accuracy                           0.78       350\n",
      "   macro avg       0.68      0.81      0.70       350\n",
      "weighted avg       0.88      0.78      0.81       350\n"
     ]
    }
   ],
   "source": [
    "print(classification_report(y_real, logit_predict))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T07:30:59.439273700Z",
     "start_time": "2025-06-11T07:30:59.428275900Z"
    }
   },
   "id": "d13f88c0ad8d57b4"
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [],
   "source": [
    "logit_predict_proba=logit.predict_proba(X_real)\n",
    "logit_predict_proba_positive=logit_predict_proba[:,1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T07:31:02.805286400Z",
     "start_time": "2025-06-11T07:31:02.797729100Z"
    }
   },
   "id": "d2536f13578a741d"
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "0.8789784638841243"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "roc_auc_score(y_real,logit_predict_proba_positive)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T07:31:03.801850100Z",
     "start_time": "2025-06-11T07:31:03.794196200Z"
    }
   },
   "id": "7e8b986be9ba2e5"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### XGBoost模型"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b6d7224b0bfd31e"
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [],
   "source": [
    "xgb=joblib.load(\"../model/xgb.pkl\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T08:07:58.115759600Z",
     "start_time": "2025-06-11T08:07:58.083878700Z"
    }
   },
   "id": "fd9f478777af59e9"
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [],
   "source": [
    "xgb_predict=xgb.predict(X_real)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T08:07:58.332771300Z",
     "start_time": "2025-06-11T08:07:58.292018100Z"
    }
   },
   "id": "91a895b855276ada"
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.90      0.96      0.93       297\n",
      "           1       0.61      0.38      0.47        53\n",
      "\n",
      "    accuracy                           0.87       350\n",
      "   macro avg       0.75      0.67      0.70       350\n",
      "weighted avg       0.85      0.87      0.86       350\n"
     ]
    }
   ],
   "source": [
    "print(classification_report(y_real, xgb_predict))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T08:08:00.511542400Z",
     "start_time": "2025-06-11T08:08:00.501272700Z"
    }
   },
   "id": "848508b58e2220ac"
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [],
   "source": [
    "xgb_predict_proba=xgb.predict_proba(X_real)\n",
    "xgb_predict_proba_positive=xgb_predict_proba[:,1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T08:08:02.758128300Z",
     "start_time": "2025-06-11T08:08:02.746106Z"
    }
   },
   "id": "b9c539c0b90ced8"
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "0.8162124388539482"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "roc_auc_score(y_real,xgb_predict_proba_positive)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-11T08:08:03.200339100Z",
     "start_time": "2025-06-11T08:08:03.190624200Z"
    }
   },
   "id": "81e06cd430f4cba3"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "6ce29f7c6154c24c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
